; Excercise 1.8
;
; Newton’s method for cube roots is based on the fact that if y is an
; approximation to the cube root of x, then a better approximation is given by
; the value 
;
; x/y^2 + 2y
; ----------
;     3 
;
; Use this formula to implement a cube-root procedure analogous to the square-
; root procedure.
; 

(define (cubert x)
  (define (square x)
    (* x x))

  (define (improve guess)
    (/ (+ (/ x (square guess))
          (* 2 guess))
       3))

  (define (in-range? lower higher x)
    (and (<= x higher) (>= x lower)))

  (define (good-enough? old-guess guess)
    (in-range? 0.999999 1.000001 (/ old-guess guess)))

  (define (cubert-iter old-guess guess)
    (if (good-enough? old-guess guess)
      guess
      (cubert-iter guess (improve guess))))

  (cubert-iter +inf 1.0))

(define (cube x) (* x x x))

; done :)

(print "The cubic root of 1e+300 is " (cubert 1e+300))
(print "The cubic root of 1e-300 is " (cubert 1e-300))

; vim: set lisp expandtabs smarttabs

